package com.xsir.ditan.miniprogram.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author user
 * @date 2021/2/7
 */
public class DateUtil {
    public static List<String> toList(String startDate, String endDate) {
        return toListUseFor(LocalDate.parse(startDate), LocalDate.parse(endDate));
    }
    
    public static List<String> toListUseStream(LocalDate startDate, LocalDate endDate) {
        return Stream.iterate(startDate, date -> date.plusDays(1))
                     .limit(ChronoUnit.DAYS.between(startDate, endDate) + 1)
                     .map(LocalDate::toString)
                     .collect(Collectors.toList());
    }
    
    public static List<String> toListUseFor(LocalDate startDate, LocalDate endDate) {
        List<String> list = new ArrayList<>();
        for(LocalDate i = startDate; i.compareTo(endDate) <= 0; i = i.plusDays(1)) {
            list.add(i.toString());
        }
        return list;
    }
    
    public static String getTimeStamp() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        return dtf.format(LocalDateTime.now());
    }
    
    public static String now() {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.format(LocalDateTime.now());
    }
}
